<template>
  <div class="goods-image">
    <!-- 预览大图 -->
    <div v-show="isShow" class="large" :style='[{backgroundImage: `url(${images[currIndex]})`},bgPosition]'></div>
    <!-- 左侧预览中图 -->
    <div class="middle" ref='target'>
      <img :src="images[currIndex]" alt="">
      <!-- 遮罩层 -->
      <div v-show="isShow" class="layer" :style="position"></div>
    </div>
    <!-- 右侧缩略图 -->
    <ul class="small">
      <li v-for="(img,i) in images" :key="img" :class="{active:i===currIndex}">
        <img @mouseenter="currIndex=i" :src="img" alt="">
      </li>
    </ul>
  </div>
</template>
<script>
import { reactive, ref, watch } from 'vue'
import { useMouseInElement } from '@vueuse/core'
export default {
  name: 'GoodsImage',
  props: {
    images: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    // 当前显示图片的索引
    const currIndex = ref(0)

    // 鼠标进去图片区的标志位
    const isShow = ref(false)

    // 遮罩层位置坐标
    const position = reactive({
      left: 0,
      top: 0
    })

    // 控制图片背景的位置
    const bgPosition = reactive({
      backgroundPostionX: 0,
      backgroundPostionY: 0
    })

    // 监听鼠标进入指定区域（实际操作DOM元素）
    const target = ref(null)
    // vueuse提供的监听方法
    const { elementX, elementY, isOutside } = useMouseInElement(target)

    // 使用wacth侦听器监听
    watch([elementX, elementY, isOutside], () => {
      // 通过标志位控制显示和隐藏
      isShow.value = !isOutside.value
      // 如果鼠标在图片区域之外移动，不需要计算坐标
      if (isOutside.value) return
      // x轴
      if (elementX.value < 100) {
        // 左
        position.left = 0
      } else if (elementX.value > 300) {
        // 右
        position.left = 200
      } else {
        position.left = elementX.value - 100
      }
      // Y方向坐标范围控制
      if (elementY.value < 100) {
      // 上侧
        position.top = 0
      } else if (elementY.value > 300) {
      // 下
        position.top = 200
      } else {
      // 中间
        position.top = elementY.value - 100
      }
      // 控制背景图位置：反方向移动2倍距离 此处的位置不能变
      bgPosition.backgroundPositionX = -2 * position.left + 'px'
      bgPosition.backgroundPositionY = -2 * position.top + 'px'

      // 添加单位
      position.left = position.left + 'px'
      position.top = position.top + 'px'
    })
    return { currIndex, isShow, target, position, bgPosition }
  }
}
</script>
<style scoped lang="less">
//大图样式
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  z-index: 500;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0,0,0,0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
  }
 //中图样式
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    //遮罩层样式
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0,0,0,.2);
      left: 0;
      top: 0;
     position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,&.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
